#pragma semicolon 1
#include <sourcemod>
#include <sdkhooks>
#include <sdktools>
#include <colors>
#include <cstrike>
#include <PowerupsX>

#define PREFIX "{olive}PowerupsX {default}>{green} "

new AbilityBanker;

new g_iBeamSprite;
new g_iHaloSprite;

public Plugin:myinfo= 
{
	name="PowerupsXL Cash",
	author="Skydive",
	description="PowerupsX Cash Ability",
	version="1.0",
	url=""
};

public OnMapStart()
{
	g_iBeamSprite = PrecacheModel("materials/sprites/laser.vmt");
	g_iHaloSprite = PrecacheModel("materials/sprites/halo01.vmt");
}

public OnPluginStart()
{
	NativeIncStart();
	
	AbilityBanker = PX_RegisterAbility("AbilityMoneyLeech");
	PX_SetAbilityName(AbilityBanker,"Money Leech");
	PX_SetAbilityDescription(AbilityBanker,"Level 5: Money Value: 1000*Level.");
	PX_SetAbilityUsage(AbilityBanker,"Level 1: Money Leeched from Victims. Level 5: Kills Drop Money");
	PX_AbilityUseColor(AbilityBanker,true);
	PX_SetAbilityColor(AbilityBanker,0,130,0,255);
	PX_SetAbilityMaxLevel(AbilityBanker,10);
	
	HookEvent("player_hurt",Event_PlayerHurt);
	HookEvent("player_death",Event_PlayerDeath);
}

public Event_PlayerDeath(Handle:event,const String:name[],bool:dontBroadcast)
{
	new victim = GetClientOfUserId(GetEventInt(event,"userid"));
	new attacker = GetClientOfUserId(GetEventInt(event, "attacker"));
	new Level = PX_GetLevel(attacker, PX_GetAbility(attacker));

	if(victim == 0) { return; }
	if(attacker == 0) { return; }
	if(Level >= 5)
	{	
		if(GetClientTeam(victim) != GetClientTeam(attacker)) // check first was attacker enemy. If not then check is mode "teamkill"
		{	
			if(PX_GetAbility(attacker) == AbilityBanker)
			{
				DoMoneyPickup(victim);
				decl Float:location[3];
				GetClientAbsOrigin(victim, location);
				TE_SetupBeamRingPoint(location, 10.0, 500.0, g_iBeamSprite, g_iHaloSprite, 0, 10, 0.6, 3.0, 0.5, { 0,200,0,200 }, 10, 0);
				TE_SendToAll();
			}	
		}
	}
}

public Action:Event_PlayerHurt(Handle:event, const String:name[],bool:dontBroadcast)
{
	new victim = GetClientOfUserId(GetEventInt(event, "userid"));
	new attacker = GetClientOfUserId(GetEventInt(event, "attacker"));
	new Level = PX_GetLevel(attacker, PX_GetAbility(attacker));
	new dhealth = GetEventInt(event, "dmg_health")*Level;
		
	if(PX_GetAbility(attacker) == AbilityBanker && Level >= 1)
	{
		if (dhealth > 0)
		{	
			if (GetEntProp(attacker,Prop_Send,"m_iAccount")+dhealth < 16000)
			{
				SetEntProp(attacker,Prop_Send,"m_iAccount",GetEntProp(attacker,Prop_Send,"m_iAccount")+dhealth);
			}
			else
			{
				SetEntProp(attacker,Prop_Send,"m_iAccount",16000);
			}
			
			if (GetEntProp(victim,Prop_Send,"m_iAccount")+(dhealth*2) < 0)
			{
				SetEntProp(victim,Prop_Send,"m_iAccount",GetEntProp(victim,Prop_Send,"m_iAccount")-(dhealth*2));
			}
			else
			{
				SetEntProp(victim,Prop_Send,"m_iAccount",0);
			}
		}
	}
}

DoMoneyPickup(client)
{
	new ent;

	if((ent = CreateEntityByName("prop_physics_multiplayer")) != -1)
	{
		new Float:pos[3], Float:vel[3], String:targetname[100];

		GetClientEyePosition(client, pos); // Let's try get position from higher

		// Random throw how Knagg0 made
		vel[0] = GetRandomFloat(-200.0, 200.0);
		vel[1] = GetRandomFloat(-200.0, 200.0);
		vel[2] = GetRandomFloat(100.0, 200.0);

		Format(targetname, sizeof(targetname), "abilitymoneypickup_%i", ent); // Create name
		DispatchKeyValue(ent, "model", "models/props/cs_assault/money.mdl");
		DispatchKeyValue(ent, "physicsmode", "2"); 
		DispatchKeyValue(ent, "massScale", "1.0"); 
		DispatchKeyValue(ent, "targetname", targetname); 
		DispatchSpawn(ent); 

		TeleportEntity(ent, pos, NULL_VECTOR, vel);
		SetEntProp(ent, Prop_Send, "m_CollisionGroup", 1);
		
		SetEntProp(ent, Prop_Send, "m_usSolidFlags", 8);
		
		SetEntPropEnt(ent, Prop_Send, "m_hOwnerEntity", client);
		
		SDKHook(ent, SDKHook_TouchPost, StartUseMoneyPost);
	}
}

public StartUseMoneyPost(entity, activator, caller, UseType:type, Float:value)
{
	if(activator > 0 && activator <= MaxClients)
	{
		new Owner = GetEntPropEnt(entity, Prop_Send, "m_hOwnerEntity");
		new Level = PX_GetLevel(Owner, PX_GetAbility(Owner));
		new Money = Level*1000;
		if (GetEntProp(activator,Prop_Send,"m_iAccount")+Money < 16000)
		{
			ClientCommand(activator, "playgamesound items/smallmedkit1.wav");

			AcceptEntityInput(entity, "Kill");
			
			SetEntProp(activator,Prop_Send,"m_iAccount",GetEntProp(activator,Prop_Send,"m_iAccount")+Money);
			
			CPrintToChat(activator,"%sYou Have Gained +%i Money!", PREFIX, Money);
		}
	
	}
}